package com.wswy.wzcx.widget.drawable

import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R

class PicFrameDrawable(private val context: Context) : Drawable() {

    private val color = ContextCompat.getColor(context, R.color.colorPrimary_70)

    private val paint= Paint(Paint.ANTI_ALIAS_FLAG)

    private val rect = Rect()

    override fun onBoundsChange(bounds: Rect?) {
        super.onBoundsChange(bounds)
        bounds?.let {
            rect.set(bounds)
        }
    }

    private val rdHeight = SizeUtils.dp2px(20f).toFloat()
    private val strokeWidth = SizeUtils.dp2px(2f).toFloat()
    private val radius = SizeUtils.dp2px(10f).toFloat()

    private val tmpRectF = RectF()

    override fun draw(canvas: Canvas?) {
        canvas?.let {

            paint.strokeWidth = strokeWidth
            paint.color = color
            paint.style = Paint.Style.STROKE

            tmpRectF.set(rect)
            tmpRectF.inset(strokeWidth,strokeWidth)
            canvas.drawRoundRect(tmpRectF,radius,radius,paint)

            paint.color = Color.WHITE
            paint.style = Paint.Style.FILL

            tmpRectF.set(rect)
            tmpRectF.inset(rdHeight,0f)
            canvas.drawRect(tmpRectF,paint)

            tmpRectF.set(rect)
            tmpRectF.inset(0f,rdHeight)
            canvas.drawRect(tmpRectF,paint)

        }
    }

    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    override fun getOpacity(): Int = PixelFormat.TRANSLUCENT

    override fun setColorFilter(colorFilter: ColorFilter?) {
        paint.colorFilter = colorFilter
    }

    override fun getIntrinsicHeight(): Int {
        return rect.height()
    }

    override fun getIntrinsicWidth(): Int {
        return rect.width()
    }

}